package com.itheima.config;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @program: itheima-technology-platform
 * @description: 构建elasticSearch客户端连接配置
 * @author: zhanghz001
 * @create: 2021-05-15 09:31
 **/
// @Configuration
// @ConfigurationProperties(prefix = "elasticsearch")
@Setter
@Getter
public class ZhzElasticSearchConfig {
    
    //es集群ip
    private String cluster_host;
    //es集群节点一端口
    private int eNode1_port;
    //es集群节点二端口    
    private int eNode2_port;
    //es集群节点三端口    
    private int eNode3_port;
    
    /**
     * 超时时间5分钟
     */
    private static final int TIME_OUT = 5 * 60 * 1000;
    
    /*
     * @Description:
     * @Method: Es高阶客户端构建器
     * @Param:
     * @Update:
     * @since: 1.0.0
     * @Return:
     *
     */
    @Bean
    public RestClientBuilder restClientBuilder() {
        return RestClient.builder(
                new HttpHost(cluster_host, eNode1_port, "http"),
                new HttpHost(cluster_host, eNode2_port, "http"),
                new HttpHost(cluster_host, eNode3_port, "http")
        );
    }
    
    /**
     * ES高级客户端构造
     *
     * @param restClientBuilder
     * @return
     */
    @Bean(destroyMethod = "close")
    public RestHighLevelClient highLevelClient(@Autowired RestClientBuilder restClientBuilder) {
        restClientBuilder.setRequestConfigCallback(
                new RestClientBuilder.RequestConfigCallback() {
                    
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        
                        return builder.setSocketTimeout(TIME_OUT);
                    }
                }
        );
        return new RestHighLevelClient(restClientBuilder);
    }
    
}
